/****

    activequant - activestocks.eu

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

	
	contact  : contact@activestocks.eu
    homepage : http://www.activestocks.eu

 ****/
package org.activequant.data.preparation;

import java.util.Arrays;
import java.util.Vector;

import org.activequant.core.domainmodel.data.MarketDataEntity;
import org.activequant.core.domainmodel.data.TimeSeries;
import org.activequant.core.util.TimeSeriesUtils;

/**
 * An abstract class, implements the IChain interface. Holds the following associated variable:
 * <ul>
 * <li>transforms(Vector&lt;IFilter&gt;)</li>
 * </ul>
 * <br>
 *       <b>History:</b><br>
 *       - [02.05.2007] Created (Erik Nijkamp)<br>
 * 
 * @author Erik Nijkamp
 */
public abstract class ChainBase implements IChain {
	/**
	 * protected Vector&lt;IFilter&gt; transforms = new Vector&lt;IFilter&gt;();
	 */
	protected Vector<IFilter> transforms = new Vector<IFilter>();
	/**
	 * constructs a ChainBase(implements IChain) using the given transforms(IFilter...) to set its associated transforms(Vector&lt;IFilter&gt;)
	 * @param transforms
	 */
	public ChainBase(IFilter... transforms) {
		this.transforms.addAll(Arrays.asList(transforms));
	}
	/**
	 * adds the given transform(IFilter) to the associated transforms(Vector&lt;IFilter&gt;) and then returns this ChainBase(implements IChain) instance
	 */
	public IChain setNext(IFilter transform) {
		transforms.add(transform);
		return this;
	}
	/**
	 * sets the associated transforms(Vector&lt;IFilter&gt;) with the given filters(IFilter...)
	 */
	public void setFilters(IFilter... filters) {
		transforms.clear();
		for (IFilter filter : filters)
			transforms.add(filter);
	}
	/**
	 * returns the associated transforms(Vector&lt;IFilter&gt;) as IFilter[] array
	 */
	public IFilter[] getFilters() {
		return transforms.toArray(new IFilter[] {});
	}
	/**
	 * processes the given sourceSeries(T...) with every IFilter in the associated transforms(Vector&lt;IFilter&gt;) and then returns the final T[] result
	 */
	public <U extends MarketDataEntity<U>, T extends TimeSeries<U>> T[] process(Class<U> clazz, T... sourceSeries) throws Exception {
		for (IFilter transform : transforms) {
			sourceSeries = transform.process(clazz, sourceSeries);
		}
		return sourceSeries;
	}
	/**
	 * returns a clone of the given series(T[]) (where T extends TimeSeries)
	 * @param <U>
	 * @param <T>
	 * @param clazz
	 * @param series
	 * @return
	 */
	protected <U extends MarketDataEntity<U>, T extends TimeSeries<U>> T[] cloneSeries(Class<U> clazz, T[] series) {
		// clone
		return TimeSeriesUtils.cloneSeries(clazz, series);
	}
	/**
	 * clears the elements of each TimeSeries in the given series(T[])
	 * @param <U>
	 * @param <T>
	 * @param clazz
	 * @param series
	 */
	protected <U extends MarketDataEntity<U>, T extends TimeSeries<U>> void clearSeries(Class<U> clazz, T[] series) {
		// clear
		TimeSeriesUtils.clearSeries(clazz, series);
	}
}
